/*
 * TagView.java
 * business
 *
 * Created by ChenTao on 2017/2/21.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.widget.view;

import android.content.Context;
import android.support.annotation.IntDef;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;

import com.yidiandao.business.R;
import com.yidiandao.business.data.bean.ITag;
import com.yidiandao.business.utils.UIUtils;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * 标签.
 */
public class TagView extends AppCompatTextView {

    public static final int COMMON = 0;

    public static final int INPUT = 1;

    public static final int DISPLAY = 2;

    public static final int RECOMMEND = 3;

    public static final int CATEGORY = 4;

    public static final int DISCOVERY = 5;

    public static final int PAY_WAY = 6;

    @IntDef({COMMON, INPUT, DISPLAY, RECOMMEND, CATEGORY, DISCOVERY, PAY_WAY})
    @Retention(RetentionPolicy.SOURCE)
    public @interface State {
    }

    /**
     * 标签状态.
     */
    private int mState;

    /**
     * 常用标签的背景图片资源标识.
     */
    private int mCommonBackground;

    /**
     * 推荐标签的背景图片资源标识
     */
    private int mRecommendBackground;

    private int mDiscoveryBackground;

    private int mPayWayBackground;

    /**
     * 常用标签的文本颜色资源标识.
     */
    private int mCommonTextColor;

    private int mInputBackground;

    /**
     * 输入标签的文本颜色资源标识.
     */
    private int mInputTextColor;

    /**
     * 显示标签的文本颜色资源标识.
     */
    private int mDisplayTextColor;

    private int mRecommendTextColor;

    private int mPayWayTextColor;

    /**
     * 数据.
     */
    private ITag mTagData;

    private Context mContext;

    public TagView(Context context) {
        this(context, null);
    }

    // /** 输入标签的背景图片资源标识. */
    // private int[] mRandomBackgroundColors = {
    //  R.drawable.bg_tag_1, R.drawable.bg_tag_2, R.drawable.bg_tag_3, R.drawable.bg_tag_4, R
    // .drawable.bg_tag_5
    // };

    public TagView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TagView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initVariables();
        initViews();
    }

    public void setState(@State int state) {
        mState = state;
        switch (mState) {
            case INPUT:
                setBackgroundResource(mCommonBackground);
                setTextColor(getResources().getColorStateList(mInputTextColor));
                break;
            case DISPLAY:
                setMinWidth((int) UIUtils.getDimens(R.dimen.dimen_70));
                setBackgroundResource(mCommonBackground);
                setTextColor(getResources().getColorStateList(mDisplayTextColor));
                break;
            case CATEGORY:
                setMinWidth((int) UIUtils.getDimens(R.dimen.dimen_70));
                setBackgroundResource(mCommonBackground);
                setTextColor(getResources().getColorStateList(mDisplayTextColor));
                break;
            case RECOMMEND:
                setMinWidth((int) UIUtils.getDimens(R.dimen.dimen_60));
                setBackgroundResource(mRecommendBackground);
                setTextColor(getResources().getColorStateList(mRecommendTextColor));
                break;
            case DISCOVERY:
                setMinWidth((int) UIUtils.getDimens(R.dimen.dimen_70));
                setTextColor(UIUtils.getColor(R.color.text_color_4));
                setBackgroundResource(mDiscoveryBackground);
                break;
            case PAY_WAY:
                setTextColor(getResources().getColorStateList(mPayWayTextColor));
                setBackgroundResource(mPayWayBackground);
                break;
            case COMMON:
            default:
                setTextColor(getResources().getColorStateList(mCommonTextColor));
                setBackgroundResource(mCommonBackground);
                break;
        }
    }

    public ITag getTagData() {
        return mTagData;
    }

    public void setTagData(ITag tagData) {
        mTagData = tagData;
        setText(mTagData.getName());
    }

    public String getTagDataName() {
        return mTagData.getName();
    }

    public String getTagId() {
        return mTagData.getId();
    }

    private void initViews() {
        setPadding(
                (int) UIUtils.getDimens(R.dimen.dimen_5),
                (int) UIUtils.getDimens(R.dimen.dimen_5),
                (int) UIUtils.getDimens(R.dimen.dimen_5),
                (int) UIUtils.getDimens(R.dimen.dimen_5));

        setTextSize(TypedValue.COMPLEX_UNIT_PX, (int) UIUtils.getDimens(R.dimen.dimen_13));
        setGravity(Gravity.CENTER);
    }

    private void initVariables() {
        mState = COMMON;
        mCommonBackground = R.drawable.sl_tag;
        mInputBackground = R.drawable.sl_tag;
        mRecommendBackground = R.drawable.sl_btn_invoice;
        mRecommendTextColor = R.color.sl_invoice;
        mCommonTextColor = R.color.sl_tag;
        mInputTextColor = R.color.btn_red;
        mDisplayTextColor = R.color.sl_tag;
        mDiscoveryBackground = R.drawable.bg_discovery_tag;

        mPayWayBackground = R.drawable.sl_pay_way;
        mPayWayTextColor = R.color.sl_pay_way;
    }
}
